package com.dn;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;


public class ConfigFrame extends JFrame{
	public ConfigFrame() {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedLookAndFeelException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		this.setLayout(new FlowLayout());
		JLabel title = new JLabel("DNLauncher Initial Setup");
		title.setFont(new Font(title.getFont().getFontName(), title.getFont()
				.getStyle(), 30));
		getContentPane().add(title);
		JLabel instruct = new JLabel("Browse to your \"DNLauncher.exe\" which is located in your Dragon Nest folder.");
		getContentPane().add(instruct);
		final JTextField path = new JTextField(30);
		path.setEditable(false);
		JButton browse = new JButton("Browse");
		final JPanel panel2 = new JPanel();
		panel2.add(path);
		panel2.add(browse);
		JButton submit = new JButton("Next");
		JPanel panel3 = new JPanel();
		panel3.add(submit);
		panel3.setPreferredSize(new Dimension(500,30));
		getContentPane().add(panel2);
		getContentPane().add(panel3);
		
		final JFileChooser fc = new JFileChooser();
		browse.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent arg0) {
				fc.showOpenDialog(panel2);
				File f = fc.getSelectedFile();
				path.setText(f.getPath());
			}
			
		});
		
		submit.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				Properties p = new Properties();
				File conf = new File("settings.conf");
				if (conf.exists()) {
					try {
						FileInputStream in = new FileInputStream(conf);
						p.load(in);
						in.close();
					} catch (FileNotFoundException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
				p.setProperty("location", path.getText());
				try {
					FileOutputStream out = new FileOutputStream(conf);
					p.store(out, "Settings config file, you are free to edit this file if you know what you are doing");
					System.out.println("hello");
					out.close();
				} catch (FileNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				new MainFrame();
				dispose();
			}
			
		});
		
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		this.setSize(500,175);
		this.setResizable(false);
		this.setVisible(true);
	}
}
